package workerwithdb.managers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import api.entitites.IMaster;
import api.entitites.IOrder;
import api.entitites.OrderStatus;
import api.messages.Messager;

public class ServerHandler {

	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private DBManager dbManager = new DBManager();
	private ArrayList<String> serverAnswer = new ArrayList<String>();
	private SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
	private Logger log = Logger.getLogger(ServerHandler.class);

	private String parserOrder(IOrder order) {
		String line = DBMessanger.ORDER_NUMBER + order.getNumber()
				+ DBMessanger.ORDER_BEGINING_DATE
				+ formatter.format(order.getBeginingDate())
				+ DBMessanger.ORDER_FILLING_DATE
				+ formatter.format(order.getFillingDate())
				+ DBMessanger.ORDER_DEADLINE_DATE
				+ formatter.format(order.getDeadlineDate())
				+ DBMessanger.ORDER_PRICE + order.getPrice();
		if (order.getOrderStatus() != null) {
			line = line + DBMessanger.ORDER_STATUS + order.getOrderStatus();
		}

		return line;
	}

	private String parserMaster(IMaster master) {
		String line = DBMessanger.MASTERS_NAME + master.getName()
				+ DBMessanger.MASTERS_EMPLOYMENT + master.getEmployment();
		return line;
	}

	public ArrayList<String> getEmptyPlaces() {
		try {
			serverAnswer.clear();
			Long places = dbManager.getEmptyPlaces();
			if (places == 0) {
				serverAnswer.add(DBMessanger.ALL_GARAGES_BUSY);
			} else {
				serverAnswer.add(DBMessanger.NUMBER_GARAGE + places);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> getOrderByMaster(String masterName) {
		try {
			serverAnswer.clear();
			IOrder order = dbManager.getOrderByMaster(masterName);
			serverAnswer.add(DBMessanger.THIS_MASTER_SERVICE
					+ order.getNumber());

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR + DBMessanger.ERROR_ORDER_BY_MASTER);
			log.error(e);
		}

		return serverAnswer;
	}

	public ArrayList<String> getMasterByOrder(Integer orderNumber) {
		try {
			serverAnswer.clear();
			IMaster master = dbManager.getMasterByOrder(orderNumber);
			serverAnswer.add(DBMessanger.THIS_ORDER_IS_SERVICED
					+ master.getName());

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR + DBMessanger.ORD_NOT_EXISTS);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> getEmptyPlacesOnDate(Date date) {
		try {
			serverAnswer.clear();
			Long count = dbManager.getEmptyPlacesOnDate(date);
			if (count == 0) {
				serverAnswer.add(DBMessanger.NOT_EMPTY_PLACE_ON_DATE);
			} else {
				serverAnswer.add(DBMessanger.NUMBER_GARAGE_ON_DATE + count);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> getNextFreeDate() {
		try {
			serverAnswer.clear();
			Date date = dbManager.getNextFreeDate();
			serverAnswer.add(DBMessanger.NEXT_FREE_DATE
					+ formatter.format(date));

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortOrderByBeginningDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortAllOrder(SortOrdersBy.BEGININGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortOrderByDeadlineDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortAllOrder(SortOrdersBy.DEADLINEDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortOrderByFillingDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortAllOrder(SortOrdersBy.FILLINGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortOrderByPrice() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortAllOrder(SortOrdersBy.PRICE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMasterByEmployment() {
		try {
			serverAnswer.clear();
			List<IMaster> mastersList = dbManager
					.sortMaster(SortMastersBy.EMPLOYMENT);
			for (IMaster m : mastersList) {
				serverAnswer.add(parserMaster(m));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMasterByName() {
		try {
			serverAnswer.clear();
			List<IMaster> mastersList = dbManager
					.sortMaster(SortMastersBy.NAME);
			for (IMaster m : mastersList) {
				serverAnswer.add(parserMaster(m));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadingOrderByBeginningDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortMadingOrder(SortOrdersBy.BEGININGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadingOrderByDeadlineDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortMadingOrder(SortOrdersBy.DEADLINEDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadingOrderByFillinggDate() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortMadingOrder(SortOrdersBy.FILLINGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadingOrderByPrice() {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortMadingOrder(SortOrdersBy.PRICE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortDeletedOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.DELETED, begin, end, SortOrdersBy.BEGININGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortCanceledOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortOrderByPeriod(OrderStatus.CANCELED, begin, end,
							SortOrdersBy.BEGININGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadeOrderByPeriodByBeginningDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.MADE, begin, end, SortOrdersBy.BEGININGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortDeletedOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.DELETED, begin, end, SortOrdersBy.DEADLINEDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortCanceledOrderByPeriodByDeadlineDate(
			Date begin, Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager
					.sortOrderByPeriod(OrderStatus.CANCELED, begin, end,
							SortOrdersBy.DEADLINEDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadeOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.MADE, begin, end, SortOrdersBy.DEADLINEDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortDeletedOrderByPeriodByFillingDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.DELETED, begin, end, SortOrdersBy.FILLINGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortCanceledOrderByPeriodByFillingDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.CANCELED, begin, end, SortOrdersBy.FILLINGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadeOrderByPeriodByFillingDate(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.MADE, begin, end, SortOrdersBy.FILLINGDATE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortDeletedOrderByPeriodByPrice(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.DELETED, begin, end, SortOrdersBy.PRICE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortCanceledOrderByPeriodByPrice(Date begin,
			Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.CANCELED, begin, end, SortOrdersBy.PRICE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> sortMadeOrderByPeriodByPrice(Date begin, Date end) {
		try {
			serverAnswer.clear();
			List<IOrder> ordersList = dbManager.sortOrderByPeriod(
					OrderStatus.MADE, begin, end, SortOrdersBy.PRICE);
			for (IOrder o : ordersList) {
				serverAnswer.add(parserOrder(o));
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> addMaster(String masterName) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.addMaster(masterName);
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				serverAnswer.add(DBMessanger.MASTER_EXISTS);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> deleteMaster(String masterName) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.deleteMaster(masterName);
			if (answer) {
				serverAnswer.add(DBMessanger.MASTER_DELETE);
			} else {
				serverAnswer.add(DBMessanger.ERROR);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> addGarage(Integer numberGarage) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.addGarage(numberGarage);
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> deleteGarage(Integer numberGarage) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.deleteGarage(numberGarage);
			if (answer) {
				serverAnswer.add(DBMessanger.GARAGE_DELETE);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}
		} catch (Exception e) {
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> addOrder(Integer orderNumber, Date fillingDate,
			Date deadlineDate, Date beginingDate, Integer price,
			String masterName, Integer numberGarage) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager
					.addOrder(orderNumber, fillingDate, deadlineDate,
							beginingDate, price, masterName, numberGarage);
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				serverAnswer.add(DBMessanger.ORD_EXISTS);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> markOrderDeleted(Integer numberOrder) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.markOrder(numberOrder,
					OrderStatus.DELETED);
			if (answer) {
				serverAnswer.add(DBMessanger.ORDER_STATUS_CHANGE);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> markOrderCanceled(Integer numberOrder) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.markOrder(numberOrder,
					OrderStatus.CANCELED);
			if (answer) {
				serverAnswer.add(DBMessanger.ORDER_STATUS_CHANGE);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> markOrderMade(Integer numberOrder) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.markOrder(numberOrder, OrderStatus.MADE);
			if (answer) {
				serverAnswer.add(DBMessanger.ORDER_STATUS_CHANGE);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> shiftOrdersDeadlineDate(Date newDate,
			Integer numberOrder) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.shiftOrdersDeadlineDate(newDate,
					numberOrder);
			if (answer) {
				serverAnswer.add(DBMessanger.ORDER_DATE_CHANGE);
			} else {
				serverAnswer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}

		return serverAnswer;
	}

	public ArrayList<String> cloneOrder(Integer number) {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.cloneOrder(number);
			if (answer) {
				serverAnswer.add(DBMessanger.CLONE);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.EXCEPTION_TRANSACTION);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> exportGarages() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.exportGarages();
			if (answer) {
				serverAnswer.add(Messager.EXPORT);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> exportMasters() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.exportMasters();
			if (answer) {
				serverAnswer.add(Messager.EXPORT);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> exportOrders() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.exportOrders();
			if (answer) {
				serverAnswer.add(Messager.EXPORT);
			}
		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> importGarages() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.importGarages();
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> importMasters() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.importMasters();
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

	public ArrayList<String> importOrders() {
		try {
			serverAnswer.clear();
			Boolean answer = dbManager.importOrders();
			if (answer) {
				serverAnswer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			}

		} catch (Exception e) {
			serverAnswer.clear();
			serverAnswer.add(DBMessanger.ERROR);
			log.error(e);
		}
		return serverAnswer;
	}

}
